<?php
namespace app\api\controller;
use think\Db;
use think\Queue;
use app\common\service\AnswerService;

class Questionnaire extends Base
{
    //列表
    public function get_questionList()
    {
        $user_id = $this->user_id;
        $s_id = $this->s_id;
        $list = AnswerService::getGuestions($s_id,'id,title,is_required,type,params');
        foreach($list as $k=>$v){
            if($v['type'] == 'Picker'){
                $list[$k]['params'] = json_decode($v['params'],true);
                if($v['id'] == 17){ //行业分类
                    $cate_list = db::name('cate')->where('parent_id','>',0)->field('id,title')->select();
                    $list[$k]['params']['selector'] = $cate_list;
                    array_unshift($list[$k]['params']['selector'],['id'=>0,'title'=>'请选择']);
                }else{
                    array_unshift($list[$k]['params']['selector'],'请选择');
                    
                    foreach ($list[$k]['params']['selector'] as $k1=>$v1){
                        $list[$k]['params']['selector'][$k1] = ['id'=>$k1,'title'=>$v1];
                    }
                }
                $list[$k]['picker_value'] = 0;//0为未选择
            }elseif($v['type'] == 'File'){
                $list[$k]['picker_value'] = [];
                $list[$k]['temp_value'] = [];
            }else{
                $list[$k]['picker_value'] = '';
            }
        }
        if ($list) {
            return return_json(200,"成功", $list);
        } else {
            return return_json(2002,"访问出错！", '');
        }
    }

    //答题
    public function answer()
    {
        $user_id = $this->user_id;
        $s_id = $this->s_id;

        $postdata = input('post.postdata');
        $postdata = str_replace(':0.00,','0,',$postdata); //2023年8月8日解决返回值中0.00被系统认为是字符串，无法计算问题，接口返回访问出错_peipei
        
		//dump($postdata);die;
		$data_arr = json_decode($postdata,true);
		//dump($data_arr);die;
        if(empty($data_arr)){
            return return_json(2001,'请提交答案1', '');
        }
        $list = AnswerService::getGuestions($s_id,'id');

        $data_arr_new = [];
        foreach($data_arr as $k=>$v){
            $k_arr = explode('_',$k);
            $data_arr_new[$k_arr[0]] = $v;
            $question_info = db::name('questions')->where('id',$k_arr[0])->field('type,is_required')->find();
            $type = $question_info['type'];
            if(empty($v)){
                $is_required = $question_info['is_required'];

               if($type == 'InputNumber'){
                   if($v == '0'){

                   }else{
                       if($is_required == 1){
                            return return_json(2001,'第'.($k_arr[1]+1).'题未填写'.$k_arr[0], '');
                       }
                   }
               }else{
                   if($is_required == 1){
                       return return_json(2001,'第'.($k_arr[1]+1).'题未填写'.$k_arr[0], $k_arr[1]+1);
                   }
               }
           }else{
                if($type == 'InputNumber'){
                   if(!is_numeric($v)){
                       return return_json(2001,'第'.($k_arr[1]+1).'请填写正确的数字', $k_arr[1]+1);
                   }
                }
                if($k_arr[0] == 4){
                    if(!isCheckMobile($v)){
                        return return_json(2001,'第'.($k_arr[1]+1).'请填写正确的手机号', $k_arr[1]+1);
                    }
                    $mobile1 = $v;
                }
                if($k_arr[0] == 12){
                    if(!isCheckMobile($v)){
                        return return_json(2001,'第'.($k_arr[1]+1).'请填写正确的手机号', $k_arr[1]+1);
                    }
                    $mobile2 = $v;
                }
                if($k_arr[0] == 170){ //组织机构代码
                    if(strlen($v) != 18){
                        return return_json(2001,'第'.($k_arr[1]+1).'请填写正确的组织机构代码', $k_arr[1]+1);
                    }
                }
                if($k_arr[0] == 8 || $k_arr[0] == 16){
                    $emails = explode('|',$v);
                    foreach ($emails as $ke=>$ve){
                        if(!isCheckEmail($ve)){
                            return return_json(2001,'第'.($k_arr[1]+1).'请填写正确的邮箱号', $k_arr[1]+1);
                        }
                    }
                }
				
				if($k_arr[0] == 78){ //拥有（含兼职）院士、杰青等科技领军人才数量
					if(!preg_match("/^[0-9][0-9]*$/",$v)){
					   return return_json(2001,'第'.($k_arr[1]+1).'请选择正确的选项', $k_arr[1]+1);
					}
				
				}
            }
        }
        // foreach($list as $k=>$v){
        //     if(!isset($data_arr_new[$v['id']])){
        //         return return_json(2001,'请提交完整答案', '');
        //     }
        // }
        // dump($data_arr_new);
        // dump($list);die;
        $res = AnswerService::calculation($user_id,$s_id,$postdata);
        if ($res['code'] == 200) {
            return return_json(200,"成功", $res['data']);
        } else {
            return return_json(2002,$res['msg'], '');
        }
    }



}